import { getUserInfo } from '@/api/user.js'
import router from '@/router'
const state = {
    token: localStorage.getItem('token') || '',
    userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {},
}

const mutations = {
    SET_TOKEN: (state, data) => {
        localStorage.setItem('token', data)
        state.token = data
    },
    SET_USERINFO(state, data){
		localStorage.setItem('userInfo', JSON.stringify(data))
		state.userInfo = data
	}
}

const actions = {
    getUserInfo({commit}, data){
		getUserInfo().then(res => {
			commit('SET_USERINFO', res.result)
		})
	},
    LogOut({commit}){
        commit('SET_TOKEN', '')
		localStorage.removeItem('token')
		commit('SET_USERINFO', {})
		localStorage.removeItem('userInfo')
        router.push('/login')
    }
}

export default{
    namespaced: true,
    state,
    mutations,
    actions
}